﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MapEditor.Model
{
    public class QuadTree
    {
        private const double playerX = 100;
        private const double playerY = 100;

        public TreeNode Root;
        public Dictionary<int, GameObject> dicObjects;

        public QuadTree(double Width, double Height)
        {
            Rect rect = new Rect();
            rect.X = Width/2;
            rect.Y = Height/2;
            rect.Width = Width;
            rect.Height = Height;

            Root = new TreeNode(1, null, rect);
            dicObjects = new Dictionary<int, GameObject>();
        }

        public void AddObject(GameObject obj)
        {
            dicObjects.Add(dicObjects.Count, obj);
            Root.AddGameObject(obj);
        }

        public void RemoveObject(GameObject obj)
        {
            Root.RemoveObjectInNode(obj);
        }

        public void serialize(ref String result)
        {
            _serializeObjects(ref result);
            result += "\n";

            Root.serialize(ref result);
        }

        private void _serializeObjects(ref string result)
        {
            result += dicObjects.Count + " " + playerX + " " + playerY + " ";
            foreach (GameObject obj in dicObjects.Values)
            {
                obj.serialize(ref result);
            }
            result.TrimEnd();
        }
    }
}
